package com.xiongda.service.impl;

import com.xiongda.common.ServerResponse;
import com.xiongda.dao.BlogMapper;
import com.xiongda.dao.CommentMapper;
import com.xiongda.dao.UserMapper;
import com.xiongda.pojo.BlogWithBLOBs;
import com.xiongda.pojo.CommentWithBLOBs;
import com.xiongda.pojo.User;
import com.xiongda.service.CommentService;
import com.xiongda.tools.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论服务层实现
 *
 * @author 缪琼
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public ServerResponse getComment(Integer pageNo1, Integer pageNo2, Long blogId) {
        BlogWithBLOBs blog = blogMapper.getBlog(blogId);
        // 获取该博客下的用户评论
        String commentIds = blog.getCommentIds();
        // 是否有评论
        if (commentIds != null) {
            // 获取所有楼层评论 时间排序 分页
            Page page = new Page(pageNo1, 10);
            Map<String, Object> map = page.setPageParams();
            map.put("commentIds", commentIds);
            List<CommentWithBLOBs> commentList = commentMapper.getCommentByBlog(map);
            Integer total = commentMapper.getTotal(map);
            page.setTotal(total);
            int index = page.getPageIndex() + 1;// 获取第一条数据的楼层数
            for (CommentWithBLOBs comment : commentList) {
                index++;
                comment.setIndex(index);
                // 遍历出每一个楼层的评论 获取楼层底下的回复 时间排序 分页
                Page page1 = new Page(pageNo2, 5);
                Map<String, Object> map1 = page1.setPageParams();
                map1.put("id", comment.getId());
                List<CommentWithBLOBs> list = commentMapper.getSubComment(map1);
                page1.setItems(list);
                Integer total1 = commentMapper.getTotal(map1);
                page1.setTotal(total1);
                List list1 = new ArrayList();
                list1.add(page1);
                comment.setList(list1);
            }
            page.setItems(commentList);
            return ServerResponse.createBySuccess("获取成功", page);
        }
        return null;
    }

    @Override
    public ServerResponse getHotComment(Integer pageNo, Long blogId) {
        BlogWithBLOBs blog = blogMapper.getBlog(blogId);
        // 获取该博客下的用户评论
        String commentIds = blog.getCommentIds();
        // 是否有评论
        if (commentIds != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("commentIds", commentIds);
            List<CommentWithBLOBs> commentList = commentMapper.getHotComment(map);
            for (CommentWithBLOBs comment : commentList) {
                // 遍历出每一个楼层的评论 获取楼层底下的回复 时间排序 分页
                Page page = new Page(pageNo, 5);
                Map<String, Object> map1 = page.setPageParams();
                map1.put("id", comment.getId());
                List<CommentWithBLOBs> list = commentMapper.getSubComment(map1);
                page.setItems(list);
                Integer total1 = commentMapper.getTotal(map1);
                page.setTotal(total1);
                List list1 = new ArrayList();
                list1.add(page);
                comment.setList(list1);
            }
            return ServerResponse.createBySuccess("获取成功", commentList);
        }
        return null;
    }

    @Override
    @Transactional
    public ServerResponse saveComment(CommentWithBLOBs commentWithBLOBs) {
        if (commentWithBLOBs.getUserId() == null)
            return ServerResponse.createByErrorMessage("参数错误");
        if (commentWithBLOBs.getContent() == null)
            return ServerResponse.createByErrorMessage("评论内容不能为空");
        if (commentWithBLOBs.getReplyFloorId() == null && commentWithBLOBs.getReplyCommentId() != null)
            return ServerResponse.createByErrorMessage("参数错误");
        if (commentWithBLOBs.getReplyFloorId() != null && commentWithBLOBs.getReplyCommentId() == null) {
            commentWithBLOBs.setParentIds(commentWithBLOBs.getReplyFloorId().toString());
            CommentWithBLOBs comment = commentMapper.getCommentById(commentWithBLOBs.getReplyFloorId());
            commentWithBLOBs.setReplyUserNikeName(comment.getUserNikeName());
        } else if (commentWithBLOBs.getReplyFloorId() != null && commentWithBLOBs.getReplyCommentId() != null) {
            commentWithBLOBs.setParentIds(commentWithBLOBs.getReplyFloorId() + "," + commentWithBLOBs.getReplyCommentId());
            CommentWithBLOBs comment = commentMapper.getCommentById(commentWithBLOBs.getReplyCommentId());
            commentWithBLOBs.setReplyUserNikeName(comment.getUserNikeName());
        }
        User user = new User();
        user.setId(commentWithBLOBs.getUserId());
        user = userMapper.getUser(user);
        commentWithBLOBs.setUserNikeName(user.getNickName());
        commentWithBLOBs.setUserHeadPicture(user.getHeadPicture());
        int count = commentMapper.insertSelective(commentWithBLOBs);
        if (count != 1)
            return ServerResponse.createByErrorMessage("评论失败");
        if (commentWithBLOBs.getReplyFloorId() == null && commentWithBLOBs.getReplyCommentId() == null) {
            BlogWithBLOBs blog = blogMapper.getBlog(commentWithBLOBs.getBlogId());
            blog.setCommentIds(blog.getCommentIds() + "," + commentWithBLOBs.getId());
            count = blogMapper.updateByPrimaryKeySelective(blog);
            if (count != 1)
                return ServerResponse.createByErrorMessage("评论失败");
        }
        return ServerResponse.createBySuccessMessage("评论成功");
    }

    @Override
    public ServerResponse deleteComment(Long id, Long userId) {
        CommentWithBLOBs comment = commentMapper.getCommentById(id);
        if (!comment.getUserId().equals(userId))
            return ServerResponse.createByErrorMessage("没有操作权限");
        int count;
        if (comment.getParentIds() == null)
            // 删除该条评论及该楼层底下所有的评论
            count= commentMapper.deleteFloorComment(id);
        else
            // 删除该条回复
            count = commentMapper.deleteByPrimaryKey(id);
        if (count != 1)
            return ServerResponse.createByErrorMessage("删除失败");
        return ServerResponse.createBySuccess("删除成功");
    }
}
